package com.liachen.common.util;

import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * TODO 功能描述
 *
 * @author chen.liang
 * @date 2020/1/4 16:40
 * @since V1.0
 */
@Slf4j
public class ExcelUtil {
    public static void export(List clzList, Class clz, String fileName, HttpServletResponse response) {
        Workbook workbook = DefaultExcelBuilder.of(clz).fixedTitles().build(clzList);
        try {
            AttachmentExportUtil.export(workbook, fileName, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            log.info("导出信息，fileName = {}", fileName);
        }
    }

    public static void export(Workbook workbook, String fileName, HttpServletResponse response) {
        try {
            AttachmentExportUtil.export(workbook, fileName, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            log.info("导出信息，fileName = {}", fileName);
        }
    }
}
